#ifndef MQCOMM_MQ_LOGGER_HPP_
#define MQCOMM_MQ_LOGGER_HPP_
#include <algorithm>
#include <iostream>
#include <ctime>
#include <string>

namespace logger
{
    enum
    {
        DEBUG,
        INFO,
        WARNING,
        ERROR,
        FATAL
    };

    class Logger
    {
    public:
        static Logger &instance()
        {
            static Logger logger;
            return logger;
        }
        std::ostream &log(const std::string &level_str, const std::string &file_name, int line)
        {
            time_t _timer = time(nullptr);
            struct tm *ptm = localtime(&_timer);
            char time_buf[32] = {0};
            std::string time_str;
            strftime(time_buf, 31, "%H:%M:%S", ptm);
            std::string message = "[" + std::string(std::move(time_buf)) + "]";
            
            message += "[" + level_str.substr(level_str.rfind(':') + 1) + "]";    // 添加日志等级
            message += "[" + file_name + ":" + std::to_string(line) + "] ";       // 错误定位

            std::cout << message; // 利用cout缓冲区刷新规则（不换行不刷新）
            return std::cout;
        }
    };

} // namespace log

#define LOG(level) logger::Logger::instance().log(#level, __FILE__, __LINE__)

#define LOG_DEBUG LOG(logger::DEBUG)
#define LOG_INFO LOG(logger::INFO)
#define LOG_WARNING LOG(logger::WARNING)
#define LOG_ERROR LOG(logger::ERROR)
#define LOG_FATAL LOG(logger::FATAL)

#endif